<?php
/**
 * 计算奖励
 */
namespace app\common;
use app\api\model\BaseConfig as BaseConfigModel;
use app\common\model\Member as MemberModel;
use app\common\model\MemberMatrix as MemberMatrixModel;
use app\common\model\MemberLevel as MemberLevelModel;
use app\common\model\RangeReward as RangeRewardModel;

class reward
{

    /**
     * 计算矩阵静态奖励
     * (全网每日新增业绩 * 静态分配比例 / 个人奖励点 / 全网奖励点总数) = 今日释放奖励点总量
     * @param $member   用户列表
     * @param $levelList    等级列表
     * @param $newadd_matrix    昨天业绩
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function matrix_static($member,$levelList,$newadd_matrix){
        //新增入金 是 购买矩阵和复投矩阵
        // ( 今日新增入金 * 静态分红比例 )* 个人分红点 / 全网分红点  = 分红金额
        //全网用户总分红点
        $count_wallet = MemberModel::count_wallet('dividend');
        //静态奖励
        $static_release = BaseConfigModel::config_data('static_release');

        //昨日新增入金 * 静态分红比例
        $fenhong = bcmul($newadd_matrix,$static_release/100,2);

        $new_member = [];
        foreach ($member as &$v){

            $v['static'] = 0;
            $v['dynamic'] = 0;

            if($v['dividend'] > 0) $v['static'] = bcdiv(bcmul($fenhong,$v['dividend'],2),$count_wallet,2);
            $v['level'] = $levelList[$v['level_id']];

            $new_member[$v['id']] = [
                'id'    =>  $v['id'],
                'static'    =>  $v['static'],   //静态奖励
                'pid'   =>  $v['pid'],  //上级ID
                'dynamic'   =>  0,  //动态奖励
                'level_id'  =>  $v['level_id'], //用户等级ID
                'level' =>  [
                    'level' =>  $levelList[$v['level_id']]['level'],
                    'weighted_dividend' =>  $levelList[$v['level_id']]['weighted_dividend'],
                    'matrix_team' =>  $levelList[$v['level_id']]['matrix_team'],
                ],
                'push_num'  =>  MemberModel::recommend_num($v['id'],0),
                'reward'    =>  $v['reward'],
            ];
        }
        return $new_member;
    }

    /**
     * 计算动态收益整理
     * @param $member   所有用户
     * @return 所有用户|mixed
     */
    public static function matrix_dynamic($member){
        $dynamic_member = $member;
        //见点奖
        $Hie = RangeRewardModel::getListAll();

        $Hierar = [];
        foreach ($Hie as $v){
            for ($i = $v['min'];$i <= $v['max'];$i++){
                $Hierar[$i] = [
                    'reward'  =>  $v['reward'], //奖励
                    'direct'    =>  $v['direct'],   //直推人数
                ];
            }
        }

        //循环发放给上级用户
        foreach ($member as $k => $v){
            if(!empty($v['pid']) && $v['static'] > 0){
                $dynamic_member = self::dynamic($v['pid'],0,$v['level']['level'],$v['static'],$dynamic_member,$Hierar);
            }
        }
        return $dynamic_member;
    }

    /**
     * 计算动态奖
     * @param $pid  上级ID
     * @param $ceng 当前层级
     * @param $level    0层用户等级
     * @param $static   0层用户静态奖
     * @param $dynamic_member   所有用户
     * @return mixed
     */
    public static function dynamic($pid,$ceng,$level,$static,$dynamic_member,$Hie){
        $ceng += 1;
        if(!empty($dynamic_member[$pid])){
            $p = $dynamic_member[$pid];
            //获取见点奖利率 $Hie[$ceng]

            //给予上级见点奖
            if(!empty($Hie[$ceng]) && $p['push_num'] >= $Hie[$ceng]['direct']){
                $p['dynamic'] = bcadd($p['dynamic'],bcmul($Hie[$ceng]['reward'],$static,8),2);
            }

            //上级获取我的团队奖  上级等级 >= 0层等级
            if($p['level']['level'] >= $level && $p['level']['weighted_dividend'] > 0) $p['dynamic'] = bcadd($p['dynamic'],bcmul($p['level']['weighted_dividend']/100,$static,8),2);

            $dynamic_member[$pid] = $p;
            //递归
            if($p['pid'] > 0 && $p['pid'] != $p['id'] && !empty($Hie[$ceng+1])) $dynamic_member = self::dynamic($p['pid'],$ceng,$level,$static,$dynamic_member,$Hie);
        }

        return $dynamic_member;
    }

}